package com.tin.triper.Background;

import com.tin.triper.Constants;
import com.tin.triper.R;
import com.tin.triper.UI.MenuActivity;
import com.tin.triper.UI.RecordActivity;
import com.tin.triper.UI.TripSetupDialogActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class RecorderWidget extends AppWidgetProvider {
	public static String ACTION_WIDGET_ADD_TEXT = "ActionWidgetAddText";
	public static String ACTION_WIDGET_ADD_PHOTO = "ActionWidgetAddPhoto";
	public static String ACTION_WIDGET_ADD_VOICE = "ActionWidgetAddVoice";
	public static String ACTION_WIDGET_ADD_VIDEO = "ActionWidgetAddVideo";
	public static String ACTION_WIDGET_START_TRIP = "ActionWidgetStartTrip";
	public static String ACTION_WIDGET_TRIPER = "ActionWidgetTriper";


	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		SharedPreferences prefs = context.getSharedPreferences(
				Constants.PREFS_NAME, Context.MODE_PRIVATE);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widget_recroder);
		
		Intent addStartTriperIntent = new Intent(context, RecorderWidget.class);
		addStartTriperIntent.setAction(ACTION_WIDGET_TRIPER);

		PendingIntent addStartTriperPendingIntent = PendingIntent.getBroadcast(
				context, 0, addStartTriperIntent, 0);

		remoteViews.setOnClickPendingIntent(
				R.id.widget_recorder_button_trip, addStartTriperPendingIntent);
		
		if (prefs.getBoolean(Constants.IS_RECORDING, false)) {

			Intent addTextIntent = new Intent(context, RecorderWidget.class);
			addTextIntent.setAction(ACTION_WIDGET_ADD_TEXT);

			PendingIntent addTextPendingIntent = PendingIntent.getBroadcast(
					context, 0, addTextIntent, 0);

			remoteViews.setOnClickPendingIntent(
					R.id.widget_recorder_button_text, addTextPendingIntent);

			Intent addVoiceIntent = new Intent(context, RecorderWidget.class);
			addVoiceIntent.setAction(ACTION_WIDGET_ADD_VOICE);

			PendingIntent addVoicePendingIntent = PendingIntent.getBroadcast(
					context, 0, addVoiceIntent, 0);

			remoteViews.setOnClickPendingIntent(
					R.id.widget_recorder_button_voice, addVoicePendingIntent);

			Intent addPhotoIntent = new Intent(context, RecorderWidget.class);
			addPhotoIntent.setAction(ACTION_WIDGET_ADD_PHOTO);

			PendingIntent addPhotoPendingIntent = PendingIntent.getBroadcast(
					context, 0, addPhotoIntent, 0);

			remoteViews.setOnClickPendingIntent(
					R.id.widget_recorder_button_photo, addPhotoPendingIntent);

			Intent addVideoIntent = new Intent(context, RecorderWidget.class);
			addVideoIntent.setAction(ACTION_WIDGET_ADD_VIDEO);

			PendingIntent addVideoPendingIntent = PendingIntent.getBroadcast(
					context, 0, addVideoIntent, 0);

			remoteViews.setOnClickPendingIntent(
					R.id.widget_recorder_button_video, addVideoPendingIntent);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_video,
					View.VISIBLE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_text,
					View.VISIBLE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_voice,
					View.VISIBLE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_photo,
					View.VISIBLE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_start,
					View.GONE);
		} else {
			remoteViews.setViewVisibility(R.id.widget_recorder_button_video,
					View.GONE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_text,
					View.GONE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_voice,
					View.GONE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_photo,
					View.GONE);
			remoteViews.setViewVisibility(R.id.widget_recorder_button_start,
					View.VISIBLE);

			Intent startTripIntent = new Intent(context, RecorderWidget.class);
			startTripIntent.setAction(RecorderWidget.ACTION_WIDGET_START_TRIP);

			PendingIntent startTripPendingIntent = PendingIntent.getBroadcast(
					context, 0, startTripIntent, 0);

			remoteViews.setOnClickPendingIntent(
					R.id.widget_recorder_button_start, startTripPendingIntent);

		}
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

	}

	@Override
	public void onReceive(Context context, Intent intent) {

		final String action = intent.getAction();
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else {
			// check, if our Action was called
			if (intent.getAction().equals(ACTION_WIDGET_ADD_TEXT)) {
				Intent recrodIntent = new Intent(context, RecordActivity.class);
				recrodIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				recrodIntent.putExtra(Constants.ACTIVITY_MODE, 0);
				context.startActivity(recrodIntent);
			}
			if (intent.getAction().equals(ACTION_WIDGET_ADD_VOICE)) {
				Intent recrodIntent = new Intent(context, RecordActivity.class);
				recrodIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				recrodIntent.putExtra(Constants.ACTIVITY_MODE, 1);
				context.startActivity(recrodIntent);
			}
			if (intent.getAction().equals(ACTION_WIDGET_ADD_PHOTO)) {
				Intent recrodIntent = new Intent(context, RecordActivity.class);
				recrodIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				recrodIntent.putExtra(Constants.ACTIVITY_MODE, 2);
				context.startActivity(recrodIntent);
			}
			if (intent.getAction().equals(ACTION_WIDGET_ADD_VIDEO)) {
				Intent recrodIntent = new Intent(context, RecordActivity.class);
				recrodIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				recrodIntent.putExtra(Constants.ACTIVITY_MODE, 3);
				context.startActivity(recrodIntent);
			}
			if (intent.getAction().equals(ACTION_WIDGET_START_TRIP)) {
				Intent startTripIntent = new Intent(context,
						TripSetupDialogActivity.class);
				startTripIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				startTripIntent.putExtra(Constants.ACTIVITY_MODE, 3);
				context.startActivity(startTripIntent);
			}
			if (intent.getAction().equals(ACTION_WIDGET_TRIPER)) {
				Intent startTripIntent = new Intent(context,
						MenuActivity.class);
				startTripIntent.addFlags(Intent.FLAG_FROM_BACKGROUND
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(startTripIntent);
			}
			super.onReceive(context, intent);
		}
	}
}
